<div ng-controller="replyCtrl">
	<!-- list -->
	<div class="fade-animate" ng-if="mode(['search'])">
		<div class="single-btn top">
			<button type="button" class="btn btn-primary" ng-click="add()"><i class="fa fa-plus"></i> &{'app.common.button.add'}</button>
		</div>
		<p class="help-wx">
			<i class="fa fa-info-circle fa-fw"></i>&{'weixin.reply.notice.1'}
		</p>
		<div class="panel panel-default">
			<div class="panel-heading">自动回复列表</div>
			<table class="table table-striped table-hover">
				<colgroup>
					<col width="30%">
					<col width="55%">
					<col width="15%">
				</colgroup>
				<thead>
					<tr>
						<th>名称</th>
						<th>关键字</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tbody>
					<tr class="fade-animate" ng-repeat="reply in replys" ng-if="!empty(replys)">
						<td>
							<sapn>{{reply.name}}</sapn>
							<a href="#" ng-click="toggleValue(reply)"><i class="fa fa-fw" ng-class="{'fa-caret-down': !showValue(reply), 'fa-caret-up': showValue(reply)}"></i></a>
							<div class="fade-animate" reply type="{{reply.type}}" value="{{reply.value}}" url="@{Resources.article_get}" ng-show="showValue(reply)"></div>
						</td>
						<td>
							<span class="label label-default" ng-if="empty(reply.keywords)">默认</span>
							<span class="label label-wx" ng-class="keywordClass(kw)" ng-repeat="kw in reply.keywords">{{kw.keyword}}</span>
						</td>
						<td class="items-wx">
							<a href="#" class="item-wx" ng-click="edit(reply)"><i class="fa fa-pencil"></i> &{'app.common.button.edit'}</a>
							<a href="#" class="item-wx" ng-click="del(reply)"><i class="fa fa-trash"></i> &{'app.common.button.del'}</a>
						</td>
					</tr>
					<tr ng-if="empty(replys)">
						<td colspan="3" class="center-wx">&{'app.common.empty'}</td>
					</tr>
				</tbody>
			</table>
		</div>
		<pagination></pagination>
	</div>
	<!-- /list -->
	
	<!-- form -->
	<div class="fade-animate" ng-if="mode(['form'])">
		#{include 'Weixins/reply_form.html' /}
	</div>
	<!-- /form -->
	
	<!-- modal -->
	#{include 'Admin/modal.html' /}
	<!-- /modal -->
</div>

<script type="text/javascript">
'use strict'
var replyCtrl = ['$scope', '$http', function($scope, $http) {
	// 定义方法
	angular.extend($scope, {
		// 初始化
		// 定义变量初始值
		// 定义值监控
		init: function() {
			$scope.mode('search');
			$scope.search(1);
			$scope.reply = {};
			$scope.type = [];
			$scope.value = [];
			
			// 监控回复类型
			$scope.$watch('reply.type', function(nvalue, ovalue) {
				$scope.type[ovalue] = $scope.reply.value;
				if (angular.isDefined($scope.type[nvalue])) {
					$scope.reply.value = $scope.type[nvalue];
				}
			});
			
			// 监控回复内容
			$scope.$watch('reply.value', function(value) {
				if (angular.isUndefined(value)) return;
				if ($scope.reply.type === '02') {
					$http({
						method: 'get',
						url: '@{Resources.article_get}?type=02&id=' + value,
						headers: {'Content-Type': 'application/x-www-form-urlencoded'}
					}).success(function(data) {
						$scope.article = data;
					});
				} else if ($scope.reply.type === '03') {
					$http({
						method: 'get',
						url: '@{Resources.article_get}?type=03&id=' + value,
						headers: {'Content-Type': 'application/x-www-form-urlencoded'}
					}).success(function(data) {
						$scope.article_multi = data;
					});
				}
			});
		}, 
		
		// 自动回复列表分页查询
		search: function(page) {
			$http({
				method: 'get',
				url: '@{Weixins.reply_search}?page=' + page,
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function(data) {
				$scope.page(data, 'replys');
			});
		}, 
		
		// 切换显示回复内容
		toggleValue: function(reply) {
			$scope.value[reply.id] = ($scope.value[reply.id]) ? false : true;
		},
		
		// 判断是否显示回复内容
		showValue: function(reply) {
			return $scope.value[reply.id];
		},
		
		// 关键字颜色
		// 默认回复灰色
		// 完全匹配浅蓝色
		// 部分匹配深蓝色
		keywordClass: function(keyword) {
			if (keyword.type === '00') {
				return 'label-info';
			} else if (keyword.type === '01') {
				return 'label-primary';
			} else if (keyword.type === '99') {
				return 'label-default';
			}
		},
		
		// 添加按钮点击事件
		// 跳转进入数据表单并做数据初始化
		add: function() {
			$scope.mode('form');
			$scope.reply = {};
			$scope.type = [];
			$scope.keywords = [];
			$scope.article = undefined;
			$scope.article_multi = undefined;
		}, 
		
		// 编辑按钮点击事件
		// 设置数据表单数据并进入表单页面
		edit: function(reply) {
			$scope.mode('form');
			$scope.reply = reply;
			$scope.type = [];
			$scope.keywords = [];
			angular.forEach(reply.keywords, function(v, i) {
				$scope.keywords.push({
					id: v.keyword,
					name: v.keyword,
					checked: v.type === '01'
				});
			});
		},
		
		// 数据保存
		// 提交数据表单
		// 同时适用于添加或修改
		save: function() {
			$scope.btn('doing');
			$scope.reply.keyword = angular.toJson($scope.keywords);
			$http({
				method: 'post',
				url: '@{Weixins.reply_save}',
				data: jQuery.param($scope.reply),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function(data) {
				if (!$scope.update(data, $scope.replys)) {
					$scope.replys.splice(0, 0, data);
				}
				$scope.cancel();
				$scope.btn('wait');
			});
		}, 
		
		// 删除自动回复数据
		del: function(reply) {
			$http({
				method: 'get',
				url: '@{Weixins.reply_delete}?id=' + reply.id,
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).success(function() {
				$scope.remove(reply, $scope.replys);
			});
		}, 
		
		// 自动回复数据表单保存按钮状态判断
		// 表单存在非法数据或表单提交未返回时不允许点击按钮
		btnDisabled: function(form) {
			return (form.$invalid || $scope.btn(['doing']));
		}, 
		
		// 取消点击事件
		// 返回列表页并清空业务数据
		cancel: function() {
			$scope.mode('search');
		}, 
		
		// 弹窗内容地址设定
		// 用户定义弹窗的页面内容
		modal: function(url) {
			$scope.modalUrl = url;
		}, 
		
		// 弹窗内容选择回调
		// 弹窗文章选择确认后的回调
		setArticle: function(selected) {
			$scope.reply.value = selected.id;
		}, 
		
		// 添加关键字
		// 自动回复表单中关键字的添加
		addKeyword: function() {
			// 定义数据
			var data = {
				id: $scope.reply.keyword,
				name: $scope.reply.keyword,
				checked: false
			};
			
			// 判断并处理
			if (angular.isUndefined($scope.keywords)) $scope.keywords = [];
			if (angular.isUndefined($scope.reply.keyword) || $scope.reply.keyword === '') return;
			if ($scope.update(data, $scope.keywords)) return;
			$scope.keywords.splice(0, 0, data);
			$scope.reply.keyword = '';
		}, 
		
		// 删除关键字
		// 自动回复表单中关键字的删除
		delKeyword: function(keyword) {
			$scope.remove(keyword, $scope.keywords);
		}
	});
	
	// 初始化
	$scope.init();
}];
</script>
